<?php
/**
 *
 * ============================================================================
 * * 版权所有 度拉拉 * *
 * 网站地址: http://www.dlltrip.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author By: 倪宗锋
 * PhpStorm LoginService.php
 * Create By 2017/6/7 18:10 $
 */

namespace common\service\login;

use common\models\ShUserSystemAuth;
use common\util\Util;
use yii\db\Exception;

class LoginService
{
    public static $uid = null;

    /**
     * Des:校验是否登录
     * Name: checkLogin
     * @return array
     * @author 倪宗锋
     */
    public static function checkLogin()
    {
        $cookies = $_COOKIE;
        /**如果用户未登录**/
        $siteConfig = Util::getSiteConfig();
        if (empty($cookies['access_token'])) {
            return Util::returnArrEr('用户未登录!', '', '', 'LOGIN-100001');
        } else {
            //将token进行解密
            $cookies['access_token'] = str_replace(' ','+', $cookies['access_token']);
            $tokenInfo = Util::authCode($cookies['access_token'], 'DECODE');
            $uid = strstr($tokenInfo, '=', true);
            /**获取用户token并校验token的正确性**/
            $cache = \Yii::$app->cache;
            $tokenKey = $siteConfig['system'] . '_LOGIN_' . YII_ENV . '_' . $uid;
            $userToken = $cache->get($tokenKey);
            if ($userToken != $cookies['access_token']) {
                return Util::returnArrEr('用户未登录!', '', '', 'LOGIN-100001');
            }
            $cache->set($tokenKey, $userToken, 2592000);
            static::$uid = $uid;
        }
        return Util::returnArrSu();
    }

    /**
     * Des:设置用户登录状态
     * Name: setLogin
     * @param $uid
     * @return array
     * @author 倪宗锋
     */
    public static function setLogin($uid)
    {
        static::$uid = $uid;//设置当前登录用户ID
        $userToken = Util::authCode($uid . '=' . microtime(true), 'ENCODE');
        $cache = \Yii::$app->cache;
        $siteConfig = Util::getSiteConfig();
        $tokenKey = $siteConfig['system'] . '_LOGIN_' . YII_ENV . '_' . $uid;
        $cache->set($tokenKey, $userToken, 2592000);
        setcookie('access_token', $userToken, time() + 3600000, "/");
        setcookie('uid', $uid, time() + 3600000, "/");
        return Util::returnArrSu('', ['access_token' => $userToken]);
    }

    /**
     * Des:非授权、静默登录
     * Name: getLoginUrlUnAuth
     * @param $showUrl string 登陆后展示地址
     * @params $type int 1、微信 2、支付宝
     * @return string
     * @author 倪宗锋
     */
    public static function getLoginUrlUnAuth($showUrl, $type)
    {
        if ($type == 1) {
            $appLogin = new WechatLogin();
            $return = $appLogin->getLoginUrlUnAuth($showUrl);
        } elseif ($type == 2) {
            $appLogin = new AliLogin();
            $return = $appLogin->getLoginUrlUnAuth($showUrl);
        } else {
            return '';
        }
        return $return;
    }

    /**
     * Des:获取微信或支付宝token
     * Name: getToken
     * @param $code
     * @param int $type
     * @return array
     * @author 倪宗锋
     */
    public static function getToken($code, $type = 1)
    {
        if ($type == 1) {
            $appLogin = new WechatLogin();
            $return = $appLogin->getToken($code);
        } else if ($type == 2) {
            $appLogin = new AliLogin();
            $return = $appLogin->getToken($code);
        } else {
            return Util::returnArrEr('');
        }
        return $return;
    }
}